//
//  UIWindow+LYAdd.m
//  LYDevelopKit
//
//  Created by 李阳 on 2018/9/21.
//

#import "UIWindow+LYAdd.h"

@implementation UIWindow (LYAdd)


- (__kindof UIViewController *)currentViewController
{
    UIViewController *currentVC = [self rootViewController];
    BOOL runLoopFind = YES;
    while (runLoopFind)
    {
        if (currentVC.presentedViewController)
        {
            currentVC = currentVC.presentedViewController;
        }
        else if ([currentVC isKindOfClass:[UINavigationController class]])
        {
            UINavigationController *navVC = (UINavigationController* )currentVC;
            currentVC = [navVC.childViewControllers lastObject];
        }
        else if ([currentVC isKindOfClass:[UITabBarController class]])
        {
            UITabBarController *tabBarVC = (UITabBarController* )currentVC;
            currentVC = tabBarVC.selectedViewController;
        }
        else
        {
            NSUInteger childViewControllerCount = currentVC.childViewControllers.count;
            if (childViewControllerCount > 0)
            {
                return currentVC.childViewControllers.lastObject;
            }
            else
            {
                return currentVC;
            }
        }
    }
    return currentVC;
}


@end
